<link rel="stylesheet" href="__PUBLIC__/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">
<form  method="post" class="form-horizontal">
	<input type="hidden" name="id" value="{$role.id}">
	<div class="form-group">
		<label class="col-md-3 col-xs-12 control-label"></label>
		<div class="col-md-6 col-xs-12">
			<h2>保存角色</h2>
		</div>
	</div>

	<div class="form-group">
		<label class="col-md-3 col-xs-12 control-label">角色名称</label>
		<div class="col-md-6 col-xs-12">
			<input name="role_name" type="text" class="form-control" value="{$role.role_name}" />
		</div>
	</div>
	<div class="form-group">
		<label class="col-md-3 col-xs-12 control-label">权限列表</label>
		<div class="col-md-6 col-xs-12">
			<ul id="treeDemo" class="ztree"></ul>
		</div>
	</div>
	<div>
		<div class="col-md-3 col-xs-12"></div>
		<div class="col-md-6 col-xs-12">
			<present name="errorNote">
				<div class="alert alert-danger" role="alert">
					<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
					<strong>{$errorNote}</strong>
				</div>
			</present>
			<div class="form-group">
				<div class="col-md-6"><input type="reset" class="btn btn-block btn-default" value=" 清 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 空 " /></div>
				<div class="col-md-6"><button type="button" class="btn btn-block btn-info pull-right" onclick="save()"> 提 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 交 </button></div>
			</div>
		</div>
	</div>
	<!-- Modal -->
	<div class="modal fade" id="my-alert" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
					<h4 class="modal-title" id="myModalLabel">提示</h4>
				</div>
				<div class="modal-body" id="hint_content">
				</div>
				<div class="modal-footer">
					<button type="button" id="close_btn" class="btn btn-default" data-dismiss="modal">关闭</button>
					<!--<button type="button" class="btn btn-primary">Save changes</button>-->
				</div>
			</div>
		</div>
	</div>
</form>
<script type="text/javascript" src="__PUBLIC__/ztree/js/jquery.ztree.core.min.js"></script>
<script type="text/javascript" src="__PUBLIC__/ztree/js/jquery.ztree.excheck.min.js"></script>
<script type="text/javascript" src="__PUBLIC__/ztree/js/jquery.ztree.exedit.min.js"></script>
<script type="text/javascript">
	function getPriIds() {
		var pri_ids = [];
		var zTree = $.fn.zTree.getZTreeObj("treeDemo")
		var nodes = zTree.getCheckedNodes(true);
		for (var i = 0; nodes != null && i < nodes.length; i++) {
			var treeNode = nodes[i];
			pri_ids.push(treeNode.id);
		}
		return pri_ids;
	}
	function save() {
		var id = $("[name='id']").val();
		var url = '';
		if(id == ''){
			url = '__URL__/add';
		}else{
			url = '__URL__/edit';
		}
		var pri_ids =getPriIds();
		$.ajax({
			type: "POST",
			url: url,
			data: {
				id: $("[name='id']").val(),
				role_name: $("[name='role_name']").val(),
				pri_ids: pri_ids
			},
			dataType: "json",
			success: function (data) {
//                $(".am-modal-bd").empty().append(data.message);
//                $("#my-alert").modal();
//				alert(data.message);
				$("#hint_content").empty().append(data.message);
				$("#my-alert").modal('show');
				if(data.check){
					$('#close_btn').attr('onclick','jump()');
				}
//				if (data.check) {
////                    $('.am-modal-btn').attr("onclick", "jump()");
//				}
			},
			error: function (data) {
//                $(".am-modal-bd").empty().append("请求超时！");
//                $("#my-alert").modal();
			}
		});
	}
	$(function () {
		var setting = {
			view: {
				selectedMulti: true, //设置是否能够同时选中多个节点
				showIcon: true,      //设置是否显示节点图标
				showLine: true,      //设置是否显示节点与节点之间的连线
				showTitle: true,     //设置是否显示节点的title提示信息
			},
			data: {
				simpleData: {
					enable: true,   //设置是否启用简单数据格式（zTree支持标准数据格式跟简单数据格式，上面例子中是标准数据格式）
					idKey: "id",     //设置启用简单数据格式时id对应的属性名称
					pIdKey: "pid"    //设置启用简单数据格式时parentId对应的属性名称,ztree根据id及pid层级关系构建树结构
				}
			},
			check: {
				enable: true         //设置是否显示checkbox复选框
			},
		};
		$.ajax({
			type: "Get",
			url: "__URL__/privileges/{$role.id}",                  //ajax请求地址
			success: function (data) {
				$.fn.zTree.init($("#treeDemo"), setting, data);  //加载数据
			},
		});
	});
	function jump() {
		window.location = '__URL__/';
	}
</script>